package com.boat.starter.ruoyi.filter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;

import com.boat.starter.ruoyi.service.AdmAuthService;
import com.boat.starter.ruoyi.util.ServletUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 认证拦截器
 * 
 * @author 周江
 *
 */
@Slf4j
@Aspect
public class TokenAuthFilter {

	@Autowired
	private AdmAuthService admAuthService;

	private static final String EXECUTE_EXPR = "@annotation(com.boat.starter.ruoyi.annotation.AdmAuthLogin) "
			+ "|| @annotation(com.boat.starter.ruoyi.annotation.AdmAuthPerm)"
			+ "|| @annotation(com.boat.starter.ruoyi.annotation.AdmAuthRole)"
			+ "|| @annotation(com.boat.starter.ruoyi.annotation.AdmAuthUser)";

	@Pointcut(EXECUTE_EXPR)
	public void multipleAnnotationPointcut() {
	}

	@Before("multipleAnnotationPointcut()")
	public void process(JoinPoint joinPoint) throws Exception {
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Method method = methodSignature.getMethod();
		Annotation[] annotations = method.getAnnotations();
		
		String token = ServletUtils.getToken();
		admAuthService.auth(token, annotations);
	}

}
